// pages/patrolLog/patrolLog.js
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    boxModalShow: false,
    fridModalShow: false,
    show:true,
    selGoodsList:[],
    myPartList:[],
    rfidCode: '',
    equipCode: '',
    pileCode: '',
    hospitalId: '',
    equipTypeIndex: 0,
    equipTypeArray: ['轮椅', '担架'],
    pileBtns:['changeLocation', 'changeBrCode', 'offLine', 'maintain', 'changeBtr', 'changeParts', 'changeBox', 'updateCard', 'rfidErr', 'statusDetail','lostChair','rentInfo','insertSwitch','hookSwitch','searchTimeout'],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    let that = this
    wx.setNavigationBarTitle({
      title: options.pileCode
    })
    that.setData({
      pileCode: options.pileCode.replace(/[^0-9]/ig, ""),
      rfidCode: '',
      scrollHeight: app.getScrollHeight(0)
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.toast = this.selectComponent("#toast")
    this.personalModal = this.selectComponent("#personalModal")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.getPileInfo();
    //添加物品
    if (that.data.addGoodsFlag) {
      let list = that.data.selGoodsList
      that.setData({
        show: false,
      })
      util.print('selGoodsList',list);
      for (var i = 0; i < list.length; i++) {
        that.selGoodsAdd(list[i]);
      }
      that.data.selGoodsList = [];
      that.setData({
        show: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  selGoodsAdd: function (selectGoods) {
    let index = this.data.myPartList.findIndex(item=>item.id==selectGoods.id);
    if(index>-1){
      this.data.myPartList[index].accNum = selectGoods.accNum;
    } else {
      this.data.myPartList.push(selectGoods);
    }
    this.setData({
      myPartList: this.data.myPartList
    });
    this.data.addGoodsFlag = false;
  },
  /**
   * 清除已选择的配件
   */
  deleteMyPartList: function(){
    this.setData({
      myPartList:[]
    })
  },
  deleteParts: function (e) {
    let that = this;
    let id = e.detail;
    console.log('deleteParts', id);
    let myPartList = that.data.myPartList;
    myPartList.forEach((item, index) => {
      if (item.id == id) {
        myPartList.splice(index, 1);
      }
    })
    that.setData({
      myPartList: myPartList
    })
  },
  getPileInfo:function(){
    let that = this;
    lbbService.getPileDetailInfo({ hid: parseInt(that.data.pileCode), rfid: '' },function(res){
      let gMac = ''
      let gHid = ''
      if(res.categoryId == 18 || res.categoryId == 25 || res.gatewayMac) {
        let index = that.data.pileBtns.indexOf('gatewayEqu')
        if(index == -1) {
          that.data.pileBtns.push('gatewayEqu')
        }        
        gMac = res.gatewayMac ? res.gatewayMac : res.macId
        gHid = res.gatewayHid ? res.gatewayHid : res.pileCode.replace('Z','')
      }
      that.setData({
        pileInfo: res,
        pileCodeZ:res.pileCode,
        pileBtns: that.data.pileBtns,
        gatewayMac: gMac,
        gatewayHid: gHid
      })
      that.setType()
    })
  },
  
  setStatus: function () {
    let that = this
    let nowStat = parseInt(that.data.pileSituation.nowStat)
    if (nowStat==1){
      that.setData({
        statText: '正常'
      })
    } else if (nowStat == 0){
      that.setData({
        statText: '生产中'
      })
    } else if (nowStat == 2) {
      that.setData({
        statText: '下线'
      })
    } else if (nowStat == 3) {
      that.setData({
        statText: '维护中'
      })
    } else if (nowStat == 4) {
      that.setData({
        statText: '备用'
      })
    }
  },
  setType: function () {
    console.log('setType')
    var that = this
    if (that.data.pileSituation && that.data.pileSituation.questionTypes) {
      var typeArray = that.data.pileSituation.questionTypes.split(',')
      var typeText = ''
      typeArray.forEach(function(item){
          if (item == 'lock_switch') {
            typeText = typeText + '  开关故障'
          } else if (item == 'pile_one_minute') {
            typeText = typeText + '  一分钟 '
          } else if (item == 'restart') {
            typeText = typeText + '  重启 '
          } else if (item == 'three_days_no_scan') {
            typeText = typeText + '  三天未扫码 '
          }
      })
      that.setData({
        typeText: typeText
      })
      that.setStatus()
    }
  }
})